package com.takeeasy.platform.action;

import java.sql.Connection;
import java.sql.SQLException;

import com.takeeasy.platform.dataobject.ResponseDataObject;
import com.takeeasy.platform.exception.PersistenceRuntimeExcption;
import com.takeeasy.platform.persistence.ConnectionPool;
import com.takeeasy.platform.persistence.IPersistenceDao;
import com.takeeasy.platform.persistence.PersistenceDaoImpl;

public class BaseAction implements IAction {
	private ResponseDataObject responseData;

	private Connection connection;
	
	protected Connection getConnection() {
		if (connection!=null) {
			return connection;
		}
		connection = ConnectionPool.getConnection();
		return connection;
	}

	protected IPersistenceDao getDao() {
		IPersistenceDao dao = new PersistenceDaoImpl(getConnection());
		return dao;
	}

	public ResponseDataObject getResponseData() {
		if (responseData == null) {
			responseData = new ResponseDataObject();
		}
		return responseData;
	}

	public void setResponseData(ResponseDataObject responseData) {
		this.responseData = responseData;
	}
	
        @Override
	public void closeConnection(){
		try {
			if (connection!=null && !connection.isClosed()) {
				this.connection.close();
			}
		} catch (SQLException e) {
			throw new PersistenceRuntimeExcption(e, "数据库关闭失败，请重新启动服务器！");
		}
	}
}
